const mongoose = require("mongoose");
// const rp=require("request-promise");

//获取列表数据
const get = async ({ curPage, eachPage }) => {
    //将数据转换为number 
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    console.log(typeof curPage);

    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    };

    const cinemasModel = mongoose.model("cinemas");
    //获取当前model映射的那个集合存在文档的总数
    data.total = await cinemasModel.countDocuments();
    data.maxPage = Math.ceil(data.total / eachPage);
    //find 方法 这里用到了链式调用
    data.rows = await cinemasModel.find()
        //把gradle里的id填充成name
        .populate("gradle")
        .skip((curPage - 1) * eachPage)
        .limit(eachPage)
        .sort({ _id: -1 })
        .exec();

    //返回到 routes下的cinemas

    return data;


}

//获取前台请求_ID数据库对应数据
const getOne = async _id => {
    const cinemasModel = mongoose.model("cinemas");
    return await cinemasModel.findOne({ _id })
}

//获取前台删除请求，将对应_ids数据删除
const Delete = async _id => {
    const cinemasModel = mongoose.model("cinemas");
    await cinemasModel.remove({ _id })
    const data = { status: true };
    return data
}


//将对应id的数据修改
const change = async ({ name, phone, address, status, _id }) => {
    // 更新修改信息
    const cinemasModel = mongoose.model("cinemas");
    return await cinemasModel.updateOne({ _id }, { name: name, status: status, phone: phone, address: address });

    // const data = { status: true }
    //返回修改成功的status值
    // return data
}

// 向数据库新增影院
const add = async ({ name, phone, address, status }) => {
    const cinemasModel = mongoose.model("cinemas");
    return await cinemasModel.create({ name, phone, address, status });

    // const data = { status: true }
    //返回新增成功的status值
    // return data
}

//向数据库对应id添加图片
//query查询条件 params需要修改的数据
const update = async (query, params) => {
    const cinemasModel = mongoose.model("cinemas");
    //数据库修改了{_id}对应的影院，向里面push了img的url，然后return回调用处
    return await cinemasModel.updateOne(query, params)
}

// 删除数据 解除关联关系
const delImg = async ({ _id, url }) => {
    const cinemasModel = mongoose.model("cinemas");
    const cinemas = await cinemasModel.findOne({ _id });//拿到
    cinemas.imgs.pull(url);//删除node.js里的数据
    await cinemas.save(); //删除的数据更新到数据库
}

// 获取电影院对应放映厅的数据
const find = async ({ status, address }) => {
    console.log("in");
    // 引入放映厅模型
    const cinemasModel = mongoose.model("cinemas");
    // 判断哪个值没有选中
    if (status!="false"&&status!="true") {
        // console.log(status !== true);
        console.log(status, 1);
        console.log("address");
        // 调用model身上find方法查询并返回查询数据
        return await cinemasModel.find({ _id: address });
    } else if (!address) {
        console.log("status");
        // 调用model身上find方法查询并返回查询数据
        return await cinemasModel.find({ status:status });
    }
    // 添加文档到模型
    return await cinemasModel.find({_id: address, status:status });
    console.log(data);
}



// 排片、放映厅获取数据
//获取列表数据
const getdata = async () => {
    const cinemasModel = mongoose.model("cinemas");
    const data = await cinemasModel.find();
    return data;
}


// 模糊查询
const findInfo = async (str) => {
    const cinemasModel = mongoose.model("cinemas");
    const data = await cinemasModel.find({"name":{"$regex": `${str}`}});
    return data;
}
module.exports = { get, getOne, change, add, Delete, getdata, update, delImg, find ,findInfo}
